# -*- coding:utf-8 -*-
# @Time : 21-11-5 上午2:19 
# @Author : zdy
# @File : UILogin.py
# desc:
import os
from importQT import *
from UI.Login import Ui_Login
from UIClass.UIModifyPsw import UIModifyPsw
from Class.Log import log
from Class.Format import Serialization
from Class.UserInfo import *
from Class.Devices import *

class UILogin(QDialog, Ui_Login):
    def __init__(self, dir):
        super(UILogin, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.applyResource()
        self.btnLogin.clicked.connect(self.btnLogin_clicked)
        self.btnCancel.clicked.connect(self.btnCancel_clicked)
        if deviceType == "CS":
            self.layout().removeWidget(self.lblUser)
            self.layout().removeWidget(self.cbbName)
            self.lblUser.setVisible(False)
            self.cbbName.setVisible(False)
            self.btnSet.setContextMenuPolicy(Qt.CustomContextMenu)
            self.btnSet.customContextMenuRequested.connect(self.create_rightmenu)
        else:
            self.layout().removeWidget(self.btnSet)
            self.layout().removeWidget(self.btnCancel)
            self.btnSet.setVisible(False)
            self.layout().addWidget(self.btnCancel, 2,3,1,2)
        # self.txtName.setText("adjustor")
        self.dir = dir
        users1 = Serialization.deserialize(os.path.join(self.dir, usersPath))
        global currentUsers
        if users1 is not None:
            currentUsers.clear()
            currentUsers.update(users1)
        if len(currentUsers)>0:
            for i in currentUsers.keys():
                self.cbbName.addItem(i)
        self.cbbName.setCurrentIndex(len(currentUsers)-1)

    def create_rightmenu(self):
        self.menu = QMenu(self)
        action1 = QAction("修改密码",self)
        action1.triggered.connect(self.btnModifyPsw_clicked)
        self.menu.addAction(action1)
        self.menu.popup(QCursor.pos())

    def applyResource(self):
        self.setWindowIcon(getIcon())

    def btnLogin_clicked(self):
        passOK = False
        if self.cbbName.currentText() == goduser.name and self.txtPsw.text() == goduser.psw:
            currentuser.name = goduser.name
            currentuser.userlevel = goduser.userlevel
            passOK = True
        else:
            user = currentUsers.get(self.cbbName.currentText())
            if user is not None:
                if self.txtPsw.text() == user.psw:
                    currentuser.name = user.name
                    currentuser.userlevel = user.userlevel
                    currentuser.psw = user.psw
                    passOK = True
                    currentUsers.pop(self.cbbName.currentText())
                    currentUsers.update({user.name: user})
                    savecurrentUsers()
                else:
                    QMessageBox.information(self, "提示", "密码错误")
            else:
                QMessageBox.warning(self,"提示","用户名未登记")
        if passOK:
            self.accept()
            self.close()

    def btnCancel_clicked(self):
        self.close()

    def btnModifyPsw_clicked(self):
        user = currentUsers.get(self.cbbName.currentText())
        if user is None:
            QMessageBox.warning(self,"提示","用户名未登记")
            return
        modify = UIModifyPsw()#user.psw)
        if modify.exec() == 1:
            user.psw = modify.newPsw
            log.info("程序", "密码修改成功")

    def addUser(self):
        pass

